//
//  WmcVideoVC.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/5/20.
//

import UIKit

// MARK: - 扩展WmcVideoPlayerVC + WmcNavigatorProtocol

extension WmcVideoPlayerVC: WmcNavigatorProtocol {
    static func navigationPaths() -> [WmcNavigatorManager.Path] {
        return [.videoPlayer]
    }

    static func willMakeNavigationVC(with parms: WmcNavigatorManager.Parms) -> UIViewController {
        var vm = WmcVideoPlayerVM()
        if let viewModel = parms.context as? WmcVideoPlayerVM {
            vm = viewModel
        }
        let vc = WmcVideoPlayerVC(vm)
        return vc
    }
}

// MARK: - 扩展WmcVideoPlayerVC + WmcVMProtocol

extension WmcVideoPlayerVC: WmcVMProtocol {
    typealias VMType = WmcVideoPlayerVM
}

// MARK: - [WmcVideoPlayerVC]

class WmcVideoPlayerVC: WmcBaseVC {
    // Lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()

        // 设置播放列表
        videoPlayerView.player.appendTaskWith(urlStrings: viewModel.taskUrlStringlist)
        // 加载第一个
        videoPlayerView.player.loadFirstTask()
        // 设置单曲循环
        videoPlayerView.player.setPlayMode(.singleCycle)
    }
 
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        viewModel.viewWillAppearBlock?()
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        viewModel.viewDidAppearBlock?()
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        viewModel.viewWillDisappearBlock?()
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        viewModel.viewDidDisappearBlock?()
    }

    // Internal

    lazy var videoPlayerView: WmcVideoPlayerView = {
        let view = WmcVideoPlayerView()
        return view
    }()

    override func makeUI() {
        makeNavView()

        addSubview(videoPlayerView)
        videoPlayerView.snp.makeConstraints { make in
            make.top.equalTo(navView.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
    }

    override func backAction() {
        videoPlayerView.player.stop()
        videoPlayerView.player.destroy()
        super.backAction()
    }
}
